package android.twitter.clientroid;

import java.util.ArrayList;

import org.json.JSONObject;

import twitter4j.Twitter;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class TimeLineActivity extends ListActivity
{
	private Twitter twitter;
	private TwitterListAdapter adapter;
    
    private ServiceConnection serviceConnection = new ServiceConnection()
    {
        public void onServiceConnected(ComponentName name, IBinder service)
        {
        	System.out.println("Service connection opened");
        }

        public void onServiceDisconnected(ComponentName name)
        {
        	System.out.println("Service connection closed");
        }
    };
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		twitter = ((TwitterApplication)getApplication()).getTwitter();
		if(twitter == null)
		{
			Toast.makeText(TimeLineActivity.this, "Application access is revoked", Toast.LENGTH_LONG).show();
			Intent backIntent = new Intent(TimeLineActivity.this, LoginActivity.class);
			startActivity(backIntent);
		}
		else
		{
			ArrayList<JSONObject> jobs = TweetReader.retrieveSpecificUsersTweets(twitter);
			
			adapter = new TwitterListAdapter(this, jobs);
			setListAdapter(adapter);
			//startUpdatingTimeLine();
		}
		System.out.println("TimeLineActivity created");
	}
	
	public void startUpdatingTimeLine()
	{
		//Intent intent = new Intent(UpdateTimeLineService.class.getSimpleName());
        Intent intent = new Intent(TimeLineActivity.this, UpdateTimeLineService.class);
        startService(intent);
        bindService(intent, serviceConnection, 0);
        System.out.println("UpdateTimeLineService started");
	}
	
	public void finishUpdatingTimeLine()
	{
		//Intent intent = new Intent(UpdateTimeLineService.class.getSimpleName());
        Intent intent = new Intent(TimeLineActivity.this, UpdateTimeLineService.class);
        unbindService(serviceConnection);
        stopService(intent);
        System.out.println("UpdateTimeLineService stopped");
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater menuInflater=getMenuInflater();
    	menuInflater.inflate(R.menu.timeline_menu, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId())
    	{
    		case R.id.menu_item_tweet:
    			tweet();
    			break;
    		case R.id.menu_item_search:
    			search();
    			break;
    		case R.id.menu_item_logout:
    			logout();
    			break;
    	}
    	return super.onOptionsItemSelected(item);
    }
    
    private void tweet()
    {
    	Intent tweetIntent = new Intent(TimeLineActivity.this, SendTweetActivity.class);
		startActivity(tweetIntent);
		//finishUpdatingTimeLine();
    }
    
    private void search()
    {
    	Intent searchIntent = new Intent(TimeLineActivity.this, SearchActivity.class);
		startActivity(searchIntent);
		//finishUpdatingTimeLine();
    }

    public void logout()
    {
    	//finishUpdatingTimeLine();
    	SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove("accessTokenToken");
		editor.remove("accessTokenSecret");
		editor.commit();
		Toast.makeText(TimeLineActivity.this, "Application access is revoked", Toast.LENGTH_LONG).show();
		Intent backIntent = new Intent(TimeLineActivity.this, LoginActivity.class);
		startActivity(backIntent);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
        	logout();
        }
        return super.onKeyDown(keyCode, event);
    }

}